package com.shwread.android.qysw.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciko.code.logic.bean.AddressBookInfo;
import com.ciko.code.logic.bean.SuperBean;
import com.mywork.activity.ApproverActivity;
import com.shwread.android.qysw.bean.BusinessEvent;
import com.shwread.android.qysw10000204.R;
import com.shwread.android.ui.dialog.BusinessDialog;
import com.shwread.android.ui.dialog.SimpleListener;
import com.shwread.android.ui.widget.CircleImageView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 审批人列表适配器
 */
@SuppressLint("ResourceAsColor")
public class ApproverAdapter extends BaseExpandableListAdapter {

    private final Context context;
    private List<SuperBean> mGroupData;
    private HashMap<String, List<AddressBookInfo>> mChildData;
    private LayoutInflater mInflater;
    private ExpandableListView lvContacts;

    public ApproverAdapter(Context context, ExpandableListView lvContacts, List<SuperBean> mGroupData, HashMap<String, List<AddressBookInfo>> mChildData) {
        this.context = context;
        this.lvContacts = lvContacts;
        this.mChildData = mChildData;
        this.mGroupData = mGroupData;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getGroupCount() {
        return null != mGroupData ? mGroupData.size() : 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (null == mChildData || null == mGroupData || mGroupData.size() == 0 || null == mGroupData.get(groupPosition) || null == mChildData.get(mGroupData.get(groupPosition).getName())) {
            return 0;
        } else {
            return mChildData.get(mGroupData.get(groupPosition).getName()).size();
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mGroupData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (null == mChildData || null == mGroupData || mGroupData.size() == 0 || null == mGroupData.get(groupPosition) || null == mChildData.get(mGroupData.get(groupPosition).getName())) {
            return null;
        } else {
            return mChildData.get(mGroupData.get(groupPosition).getName()).get(childPosition);
        }
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View v;
        if (convertView == null) {
            v = mInflater.inflate(R.layout.approver_group_item, parent, false);
        } else {
            v = convertView;
        }

        final SuperBean group = mGroupData.get(groupPosition);
        TextView groupNameTV = (TextView) v.findViewById(R.id.item_expandable_group);
        final ImageView openIV = (ImageView) v.findViewById(R.id.item_img_open);
        LinearLayout llGroup = (LinearLayout) v.findViewById(R.id.ll_item_group);
        groupNameTV.setText(group.getName());
        if (ApproverActivity.isClose[groupPosition] == 0) {
            openIV.setImageResource(R.drawable.change_store_icon);
        } else {
            openIV.setImageResource(R.drawable.change_store_icon_up);
        }
        llGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ApproverActivity.isClose[groupPosition] != 0) {
                    List<AddressBookInfo> list = mChildData.get(group.getName());
                    if (list.size() > 50) {
                        List<AddressBookInfo> newList = new ArrayList<>();
                        for (int i = 0; i < 50; i++) {
                            newList.add(list.get(i));
                        }
                        mChildData.put(group.getName(), newList);
                    }
                    lvContacts.collapseGroup(groupPosition);
                    openIV.setImageResource(R.drawable.change_store_icon);
                    ApproverActivity.isClose[groupPosition] = 0;
                } else {
                    for (int i = 0; i < mGroupData.size(); i++) {
                        if (ApproverActivity.isClose[i] == 1) {
                            List<AddressBookInfo> list = mChildData.get(mGroupData.get(i).getName());
                            if (list.size() > 50) {
                                List<AddressBookInfo> newList = new ArrayList<>();
                                for (int j = 0; j < 50; j++) {
                                    newList.add(list.get(j));
                                }
                                mChildData.put(mGroupData.get(i).getName(), newList);
                            }
                            lvContacts.collapseGroup(i);
                            ApproverActivity.isClose[i] = 0;
                        }
                    }
                    lvContacts.expandGroup(groupPosition);
                    openIV.setImageResource(R.drawable.change_store_icon_up);
                    ApproverActivity.isClose[groupPosition] = 1;
                }
            }
        });
        return v;
    }

    private class ChildViewHolder {
        private CircleImageView contactsHeadImg;
        private ImageView contactsHeader;
        private TextView contactsName;
        private RelativeLayout rlItem;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.approver_contact_item, parent, false);
            holder = new ChildViewHolder();
            holder.contactsHeadImg = (CircleImageView) convertView.findViewById(R.id.iv_chat_head_img);
            holder.contactsHeader = (ImageView) convertView.findViewById(R.id.iv_chat_contact_icon_header);
            holder.contactsName = (TextView) convertView.findViewById(R.id.tv_chat_contact_name);
            holder.rlItem = (RelativeLayout) convertView.findViewById(R.id.rl_contact_item);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        final AddressBookInfo contactItem = (AddressBookInfo) getChild(groupPosition, childPosition);
        if (contactItem.getUserHead() != null && contactItem.getUserHead().length() != 0) {
            Picasso.with(context).load(contactItem.getUserHead()).into(holder.contactsHeadImg);
        } else {
            Picasso.with(context).load(R.drawable.chat_head_img).into(holder.contactsHeadImg);
        }
        if (contactItem.getGroupPosition() == 1) {
            Picasso.with(context).load(R.drawable.chat_contract_icon_header).into(holder.contactsHeader);
            holder.contactsHeader.setVisibility(View.VISIBLE);
        } else if (contactItem.getGroupPosition() == 2) {
            Picasso.with(context).load(R.drawable.chat_contract_icon_deputy_header).into(holder.contactsHeader);
            holder.contactsHeader.setVisibility(View.VISIBLE);
        } else {
            holder.contactsHeader.setVisibility(View.GONE);
        }
        holder.contactsName.setText(contactItem.getContactName());

        holder.rlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//点击事件
                //是否发起人
                boolean isOrganiser = ((ApproverActivity) context).getIntent().getBooleanExtra("isOrganiser", false);
                if (isOrganiser) {
                    Intent intent = new Intent();
                    intent.putExtra("Approver", contactItem);
                    ((ApproverActivity) context).setResult(1011, intent);
                    ((ApproverActivity) context).finish();
                }
                else {
                    BusinessDialog businessDialog =new BusinessDialog(context,new SimpleListener() {
                        @Override
                        public void ok() {
                            //EventBus发送数据
                            EventBus.getDefault().post(new BusinessEvent(Integer.parseInt(contactItem.getUserId()),contactItem.getContactName()));
                            ((ApproverActivity) context).finish();

                        }
                    });
                    businessDialog.show();
                }
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean isEmpty() {
        return mGroupData == null || mGroupData.isEmpty();
    }
}
